<!--
 * @Author: lisong
 * @Date: 2023-04-13 16:10:48
 * @LastEditors: ZHM zhuimin111@163.com
 * @LastEditTime: 2023-04-26 09:51:33
 * @Description: 表单添加渲染弹窗
-->
<template>
  <div class="common-proj-filling-wrapper">
    <div class="common-proj-filling-header">
      <div class="header-item left">
        <div class="left-item">
          <div class="left-item-label">单位名称 </div>
          <el-input class="left-item-content" disabled :value="echoFormData.agName || '暂无'" />
        </div>
      </div>
      <div class="header-item right">
        <el-button @click="handleSave">暂存</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button type="danger" @click="handleClose">取消</el-button>
      </div>
    </div>
    <div class="common-proj-filling-body">
      <div class="filling-body-center">
        <RkFormRenderBusi
          ref="rkFormRenderRef"
          :busiType="busiType"
          :formId="formId"
          :formType="buttonCode"
          :formData="echoFormData"
          @loaded="renderBusiFormLoaded"
        />
      </div>
    </div>
  </div>
</template>

<script setup name="ReserveProjGenericPanel">
import RkUtils from '@/utils/rk-utils.js'
import RkFormRenderBusi from '@/components/form/RkFormRenderBusi.vue'
import { ElMessage, ElLoading } from "element-plus"
import { saveGenericProject, getReserveProjectInfo } from '@/api/api-project.js'
import XEUtils from 'xe-utils';

const emits = defineEmits([
  'save',
  'close',
  'loaded',
  'confirm',
])

const props = defineProps({
  // 单位ID
  agId: { type: String, default: '' },
  // 区划编码
  adCode: { type: String, default: '' },
  // 单位名称
  agName: { type: String, default: '' },
  // 单位编码
  agCode: { type: String, default: '' },
  // 父级单位ID
  agParentId: { type: String, default: '' },
  // 项目ID
  proId: {type: String, default: RkUtils.getGuid()},
  // 表单ID
  formId: { type: String, default: '' },
  // 是否预算单位: 1 是 2 否
  isBgtAg: { type: String, default: '1' },
  // 项目来源 1：基础库 2：储备库 3:需求库 4：历史项目 5、一体化同步项目 6、存量债务无项目生成项目
  proSource: { type: String, default: '2' },
  // 流程标识
  processKey: { type: String, default: '2' },
  // 面板类型
  buttonCode: { type: String, default: 'add' },
  // 附件规则
  busiType: { type: String, default: 'ET001' }
})

// 是否加载中
const loading = ref(false)
// 提交的参数-也是传递的参数
const submitParams = ref({})
// 表单回显数据
const echoFormData = ref({})
// 打开加载中
const openLoading = ref({})
// 表单渲染模板引用对象
const rkFormRenderRef = ref()

/**
 * 加载数据
 * @param {Object} config 配置参数
 */
const loadData = async(config) => {
  if (!config.proId) config.proId=RkUtils.getGuid()
  submitParams.value = config
  echoFormData.value = config
  loading.value = true
  openLoading.value = ElLoading.service({ fullscreen: true, text: '加载中......' })
  if (config.buttonCode !== 'add') {
    const projectInfo = await getProjectInfo(config.proId, config.formId)
    // 赋值
    echoFormData.value = { ...echoFormData.value, ...projectInfo }
    // 对单位名称赋值
    echoFormData.value['agName'] = projectInfo['AGENCY_NAME'] || config['agName']
  } else {
    // 基本信息-主管部门
    echoFormData.value['DEPTARTMENT_ID'] = config['agParentId']
    // 基本信息-预算单位
    echoFormData.value['AGENCY_CODE'] = config['agCode']
    // 前期准备-项目的资产登记管理单位
    echoFormData.value['PROMANAGER_AG_CODE'] = config['agCode']
  }
}
const handleSave = () => {
  emitSave()
  ElMessage.info('功能正在开发中......')
}
const handleClose = () => {
  emitClose()
}
const handleConfirm = () => {
  const { getRenderFormData } = rkFormRenderRef.value
  getRenderFormData().then((params) => {
    const { dataArray, slotsData} = params
    const { isBgtAg } = props
    const result = {
      data: dataArray, ...submitParams.value, ...slotsData
    }
    // 封装数据
    result['AGENCY_ID'] = isBgtAg === '1' ? result.agId : ''
    result['AGENCY_CODE'] = isBgtAg === '1' ? result.agCode : ''
    result['AGENCY_NAME'] = isBgtAg === '1' ? result.agName : ''
    result['NON_AGENCY_ID'] = result.agId
    result['NON_AGENCY_CODE'] = result.agCode
    result['NON_AGENCY_NAME'] = result.agName
    const saveLoading = ElLoading.service({ fullscreen: true, text: '信息正在保存中......' })
    saveGenericProject(result).then(res => {
      const { code, data } = res
      if (code === 200) {
        emitConfirm()
        ElMessage.success('保存成功！')
      } else {
        ElMessage.error('保存失败！')
      }
    }).finally(() => {
      saveLoading.close()
    })
  })
}

/**
 * 获取表单回显信息
 * @param {String} objectId 回显数据
 */
const getProjectInfo = (proId, formId) => {
  return new Promise(resolve => {
    getReserveProjectInfo({ proId, formId }).then((res) => {
      const { code, data, msg } = res
      if (code === 200) {
        resolve(data)
      } else {
        console.error(msg)
        resolve({})
      }
    }).catch(err => {
      console.error(err)
      openLoading.value.close()
      loading.value = false
      resolve({})
    })
  })
}

// ----- 表单渲染相关内容
const renderBusiFormLoaded = () => {
  openLoading.value.close()
  loading.value = false
  emitLoaded()
}

// ----- emits相关调用
const emitSave = () => {
  emits('save')
}
const emitClose = () => {
  emits('close')
}
const emitLoaded = () => {
  emits('loaded')
}
const emitConfirm = () => {
  emits('confirm')
}

loadData(XEUtils.clone(props, true))

</script>


<style lang="scss" scoped>
</style>
